/*
 *************************************************************************
 * The contents of this file are subject to the Openbravo  Public  License
 * Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
 * Version 1.1  with a permitted attribution clause; you may not  use this
 * file except in compliance with the License. You  may  obtain  a copy of
 * the License at http://www.openbravo.com/legal/license.html 
 * Software distributed under the License  is  distributed  on  an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific  language  governing  rights  and  limitations
 * under the License. 
 * The Original Code is Openbravo ERP. 
 * The Initial Developer of the Original Code is Openbravo SL 
 * All portions are Copyright (C) 2001-2006 Openbravo SL 
 * All Rights Reserved. 
 * Contributor(s):  ______________________________________.
 ************************************************************************
*/
package org.openbravo.wad.controls;

import java.util.*;

public class WADGrid extends WADControl {

  public WADGrid() {
  }

  public WADGrid(Properties prop) {
    setInfo(prop);
    initialize();
  }

  public void initialize() {
    //addCSSImport("DataGrid", "");
    //addImport("dojo", "../../../../../web/js/dojo/dojo.js");
    generateJSCode();
    setValidation("");
  }

  private void generateJSCode() {
    addJSCode("DataGrid", "dojo.require(\"openbravo.widget.DataGrid\");");
    StringBuffer text = new StringBuffer();
    {
      text.append("function updateHeader(liveGrid, offset) {\n");
      text.append("  dojo.byId('bookmark').innerHTML = (offset+1) + \" - \"");
      text.append("+ (offset+liveGrid.visibleRows) + \" / \" + liveGrid.metaData.getTotalRows();\n");
      text.append("}");
      addJSCode("updateHeader", text.toString());
    }
    {
      text = new StringBuffer();
      text.append("function onRowDblClick(cell) {\n");
      text.append("  var value = dojo.widget.byId('").append(getData("id")).append("').getSelectedRows();\n");
      text.append("  if (value==null || value==\"\" || value.length>1) return false;\n");
      text.append("  setInputValue('").append(getData("inpKeyName")).append("', value);\n");
      text.append("  return submitCommandForm('EDIT', true, null, document.frmMain.urlwin.value, '_self');\n");
      text.append("}");
      addJSCode("onRowDblClick", text.toString());
    }
    {
      text = new StringBuffer();
      text.append("function getSelectedValues() {\n");
      text.append("  var value = dojo.widget.byId('").append(getData("id")).append("').getSelectedRows();\n");
      text.append("  if (value==null || value.length==0) return \"\";\n");
      text.append("  return value[0];\n"); 
      text.append("}");
      addJSCode("getSelectedValues", text.toString());
    }
    {
      text = new StringBuffer();
      text.append("function isMultipleSelected() {\n");
      text.append("  var value = dojo.widget.byId('").append(getData("id")).append("').getSelectedRows();\n");
      text.append("  if (value==null || value==\"\") return false;\n");
      text.append("  return (value.length>1);\n");
      text.append("}");
      addJSCode("isMultipleSelected", text.toString());
    }
    {
      text = new StringBuffer();
      text.append("function onGridLoadDo() {\n");
      text.append("  if (selectedRow==null) return true;\n");
      text.append("  if (selectedRow<=0) dojo.widget.byId('").append(getData("id")).append("').goToFirstRow();\n");
      text.append("  else dojo.widget.byId('").append(getData("id")).append("').goToRow(selectedRow);\n");
      text.append("  if (orderByPositions!=null && orderByPositions.length>0) {\n");
      text.append("    var total = orderByPositions.length;\n");
      text.append("    for (var i=0;i<total;i++) {\n");
      text.append("      dojo.widget.byId('").append(getData("id")).append("').setSortedColumns(orderByPositions[i], orderByDirections[i]);\n");
      text.append("    }\n");
      text.append("  }\n");
      text.append("  dojo.widget.byId('").append(getData("id")).append("').setFocus();\n");
      text.append("  return true;\n");
      text.append("}");
      addJSCode("onGridLoadDo", text.toString());
    }
    {
      text = new StringBuffer();
      text.append("function setGridFilters(newparams) {\n");
      text.append("  var params = [];\n");
      text.append("  params[\"newFilter\"] = \"1\";\n");
      text.append("  if (newparams!=null && newparams.length>0) {\n");
      text.append("    var total = newparams.length;\n");
      text.append("    for (var i=0;i<total;i++) {\n");
      text.append("      params[newparams[i][0]] = newparams[i][1];\n");
      text.append("    }\n");
      text.append("  }\n");
      text.append("  dojo.widget.byId('").append(getData("id")).append("').setRequestParams(params);\n");
      text.append("  return true;\n");
      text.append("}");
      addJSCode("setGridFilters", text.toString());
    }
    {
      text = new StringBuffer();
      text.append("function updateGridData() {\n");
      text.append("  dojo.widget.byId('").append(getData("id")).append("').refreshGridData();\n");
      text.append("  return true;\n");
      text.append("}");
      addJSCode("updateGridData", text.toString());
    }
    {
      text = new StringBuffer();
      text.append("function updateGridDataAfterFilter() {\n");
      text.append("  dojo.widget.byId('").append(getData("id")).append("').refreshGridDataAfterFilter();\n");
      text.append("  return true;\n");
      text.append("}");
      addJSCode("updateGridDataAfterFilter", text.toString());
    }
  }

  public String toString() {
    StringBuffer text = new StringBuffer();
    text.append("<TABLE width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" class=\"Main_Client_TableRelation\"><TR><TD>");
    text.append("<div id=\"").append(getData("id")).append("\" dojoType=\"openbravo:DataGrid\"\n");
    text.append("      structureUrl=\"../utility/DataGrid.html?Command=STRUCTURE&inpadTabId=");
    text.append(getData("AD_Tab_ID")).append("&inpadWindowId=");
    text.append(getData("AD_Window_ID")).append("\" \n");
    text.append("      dataUrl=\"../utility/DataGrid.html?Command=DATA&inpadTabId=");
    text.append(getData("AD_Tab_ID")).append("&inpadWindowId=");
    text.append(getData("AD_Window_ID")).append("\" \n");
    text.append("      updatesUrl=\"../utility/DataGrid.html?Command=UPDATE&inpadTabId=");
    text.append(getData("AD_Tab_ID")).append("&inpadWindowId=");
    text.append(getData("AD_Window_ID")).append("\" \n");
    text.append("      numRows=\"").append(getData("NumRows")).append("\" \n");
    text.append("      editable=\"").append(getData("editable")).append("\" sortable=\"").append(getData("sortable")).append("\" \n");
    text.append("      deleteable=\"").append(getData("deleteable")).append("\" \n");
    text.append("      onInvalidValue=\"alert\" \n");
    text.append("      onScroll=\"").append(getData("onScrollFunction")).append("\" \n");
    text.append("      onGridLoad=\"").append(getData("onLoadFunction")).append("\"\n");
    text.append("      bufferSize=\"3.0\"\n");
    text.append("      showLineNumbers=\"").append(getData("ShowLineNumbers")).append("\" \n");
    text.append("      maxWidth=\"").append(getData("width")).append("\" preventCache=\"true\" useCache=\"true\" cacheContent=\"false\">\n");
    text.append("    </div>\n");
    text.append("<script>djConfig.searchIds.push(\"").append(getData("id")).append("\");</script>");
    text.append("</TD></TR></TABLE>");
    return text.toString();
  }

  public String toXml() {
    return "<PARAMETER id=\"" + getData("id") + "\" name=\"" + getData("id") + "\" attribute=\"numrows\"/>";
  }

  public String toJava() {
    return "xmlDocument.setParameter(\"" + getData("id") + "\", Utility.getContext(this, vars, \"#RecordRange\", windowId));";
  }
}
